﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;
using Gacn.ControleAlunos.Library.Entities;

namespace Gacn.ControleAlunos.Library.DataAccess
{
    internal static class EscolaDataAccess
    {

        public static DataTable GetAllEscolaEntity()
        {
            SqlConnection connection = null;
            SqlCommand command = null;
            DataTable dataTable = null;

            connection = Util.GetConnection();
            connection.Open();

            command = connection.CreateCommand();
            command.CommandText = "select idEscola, NomeEscola from tbEscola where flgAtivo = 1";
            command.CommandType = CommandType.Text;

            dataTable = new DataTable();
            dataTable.Locale = CultureInfo.InvariantCulture;
            new SqlDataAdapter(command).Fill(dataTable);

            return dataTable;
        }

        public static DataTable CadastraEscola(EscolaDetalhadaEntity escola)
        {
            SqlConnection connection = null;
            SqlCommand command = null;
            DataTable dataTable = null;


            connection = Util.GetConnection();
            connection.Open();

            command = connection.CreateCommand();
            command.CommandText = "uspCadastraEscola";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add("Bairro", SqlDbType.Int).Value = escola.Bairro;
            command.Parameters.Add("CEP", SqlDbType.Int).Value = escola.CEP;
            command.Parameters.Add("idCidade", SqlDbType.Int).Value = escola.idCidade;
            command.Parameters.Add("idEstado", SqlDbType.Int).Value = escola.idEstado;
            command.Parameters.Add("Logradouro", SqlDbType.Int).Value = escola.Logradouro;
            command.Parameters.Add("NomeEscola", SqlDbType.Int).Value = escola.Nome;
            command.Parameters.Add("obs", SqlDbType.Int).Value = escola.Observacao;
            command.Parameters.Add("Telefone", SqlDbType.Int).Value = escola.Telefone;

            command.Parameters.Add("SUCESSO", SqlDbType.Bit).Direction = ParameterDirection.Output;
            command.Parameters.Add("ProcComment", SqlDbType.VarChar).Direction = ParameterDirection.Output;

            dataTable = new DataTable();
            dataTable.Locale = CultureInfo.InvariantCulture;
            new SqlDataAdapter(command).Fill(dataTable);

            if (!(bool)command.Parameters["SUCESSO"].Value)
            {
                throw new Exception(string.Concat("Ocorreu um erro ao executar a procedure. \r\n", command.Parameters["ProcComment"].Value.ToString()));
            }

            return dataTable;
        }
    }
}
